package cn.cvs.controller;

import cn.cvs.pojo.StorageRecord;
import cn.cvs.pojo.Supplier;
import cn.cvs.pojo.SysRole;
import cn.cvs.pojo.SysUser;
import cn.cvs.service.StorageRecordService;
import cn.cvs.service.SupplierService;
import cn.cvs.service.SysUserService;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping({"/storageRecord"})
public class StorageRecordController {
    private Logger logger=Logger.getLogger(StorageRecordController.class);
    @Resource
    private StorageRecordService storageRecordService;
    @Resource
    private SupplierService supplierService;

    @RequestMapping({"/list"})
    public String list(Model model, @RequestParam(value = "queryGoodsName",required = false) String queryGoodsName, @RequestParam(value = "querySupplierId",required = false) Integer querySupplierId, @RequestParam(value = "queryPayStatus",required = false) Integer queryPayStatus, @RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex) {
        List<Supplier> supplierList = null;
        List storageRecordList = null;

        try {
            PageHelper.startPage(pageIndex, 5);
             try {
                storageRecordList = this.storageRecordService.getList(queryGoodsName, querySupplierId, queryPayStatus);
                supplierList = this.supplierService.getList();

            } catch (Exception var13) {
                var13.printStackTrace();
            }
            PageInfo<StorageRecord> pageInfo = new PageInfo<>(storageRecordList);
            model.addAttribute("storageRecordList", storageRecordList);
            model.addAttribute("supplierList", supplierList);
            model.addAttribute("queryGoodsName", queryGoodsName);
            model.addAttribute("querySupplierId", querySupplierId);
            model.addAttribute("queryPayStatus", queryPayStatus);
            model.addAttribute("page",pageInfo);
            return "storageRecord/list";
        } catch (Exception var14) {
            var14.printStackTrace();
            return "redirect:/toSysError";
        }
    }


    @GetMapping({"/toAdd"})
    public String addUser() {
        return "storageRecord/add";
    }

    @PostMapping("/add")
    public String addUserSave(StorageRecord storageRecord,HttpServletRequest request) throws Exception {
        storageRecord.setCreatedUserId(((SysUser) request.getSession().getAttribute("userSession")).getId());
        int count=storageRecordService.add(storageRecord);
        if (count>0) {
            return "redirect:/storageRecord/list";
        }
        return "/storageRecord/add";
    }

    @GetMapping({"/view/{id}"})
    public String view(@PathVariable Integer id, Model model) throws Exception {
        this.logger.debug("根据id查询用户详情" + id);
        StorageRecord storageRecord = this.storageRecordService.getById(id);
        model.addAttribute(storageRecord);
        return "storageRecord/view";
    }

    @GetMapping({"/toUpdate/{id}"})
    public String toUpdate(@PathVariable Integer id, Model model) throws Exception {
        StorageRecord storageRecord = this.storageRecordService.getById(id);
        model.addAttribute(storageRecord);
        return "storageRecord/update";
    }

    @PostMapping({"/update"})
    public String update(StorageRecord storageRecord, HttpSession session) throws Exception {
        this.logger.debug("修改供货商信息supplier:" + storageRecord);
        storageRecord.setCreatedUserId(((SysUser)session.getAttribute("userSession")).getId());
        return this.storageRecordService.update(storageRecord) ? "redirect:/storageRecord/list" : "storageRecord/update";
    }

    @DeleteMapping({"/del/{id}"})
    @ResponseBody  //  作用：将方法的返回值，以特定的格式写入到response的body区域，进而将数据返回给客户端。
    public Object del(@PathVariable(value = "id",required = false) Integer id) throws Exception //PathVariable获取URL中路径的参数值
    {
        HashMap resultMap = new HashMap();
        int count=storageRecordService.deleteById(id);
        try {
            if (count>0) {
                resultMap.put("delResult", "true");
            } else {
                resultMap.put("delResult", "false");
            }
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        return JSONArray.toJSONString(resultMap);
    }


}
